package tesis.sin.rest.parser;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class SerializadorObjetos extends JsonSerializer<Object>{

	private List<String> atributos;

	public SerializadorObjetos(String... atributos)
	{
		this.atributos = Arrays.asList(atributos);
	}

	@Override
	public void serialize(Object objeto, JsonGenerator jGen, SerializerProvider arg2) throws IOException, JsonProcessingException {
		try
		{
			jGen.writeStartObject(); 
			BeanInfo info = Introspector.getBeanInfo(objeto.getClass());  
	        PropertyDescriptor[] props = info.getPropertyDescriptors();  
	        for(PropertyDescriptor pd : props) 
	        {  
	            if(!atributos.isEmpty() && atributos.contains(pd.getName()))
	                jGen.writeStringField(pd.getName(), pd.getReadMethod().invoke(objeto, null).toString());
	        }
	        jGen.writeEndObject();
		}
		catch (Exception e) {}
	}
}
